<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dssp="http://schemas.microsoft.com/xw/2004/10/dssp.html"
    xmlns:s="http://www.w3.org/2003/05/soap-envelope"
    xmlns:limit="http://schemas.vexrobotics.com/2011/09/vexservice.limitswitch.html" >

  <xsl:import href="/resources/dss/Microsoft.Dss.Runtime.Home.MasterPage.xslt" />

  <xsl:template match="/">
    <xsl:comment>
      <!-- Service Header Info -->
    </xsl:comment>
    <xsl:variable name="title">
      VEX Limit Switch
    </xsl:variable>
    <xsl:variable name="serviceName">
      VEX Limit Switch
    </xsl:variable>
    <xsl:variable name="description">
      Provides access to the VEX Limit switch.
    </xsl:variable>

    <xsl:call-template name="MasterPage">
      <xsl:with-param name="serviceName" select="$serviceName" />
      <xsl:with-param name="description" select="$description" />
      <!-- If title is not provided, serviceName will be used instead. -->
      <xsl:with-param name="title">
        <xsl:value-of select="$serviceName" />
        <xsl:if test="$title != ''">
          <xsl:text> - </xsl:text>
          <xsl:value-of select="$title" />
        </xsl:if>
      </xsl:with-param>
      <!-- Possible values for navigation are: 'Open', 'Closed', and 'None'
           'Open' is the default value. -->
      <xsl:with-param name="navigation" select="'Open'" />
      <!-- The contents of head param will be placed just before the </head> tag in html. -->
      <xsl:with-param name="head">
        <style type="text/css">
          /* Service-specific stylesheet goes here */
        </style>
        <script language="javascript" type="text/javascript" src="/resources/VEXService.Y2011.M09/VEXService.config.Common.js"/>
        <script language="javascript" type="text/javascript">
          <xsl:text>
var pollingFrequency = 1000;
var timeStamp = "</xsl:text>
          <xsl:value-of select="/limit:VEXContactSensor/limit:TimeStamp"/>
          <xsl:text>";
</xsl:text>

          <![CDATA[
          
function onState(state)
{
    if (state.TimeStamp != timeStamp)
    {
      timeStamp = state.TimeStamp;
      document.all("timestampCell").innerText = timeStamp;
      
      document.all("pressedCell").innerText = state.Pressed;
      if(state.Pressed != "false")
      {
        document.all("pressedCell").style.backgroundColor = "#9ecba3";
      }
      else
      {
        document.all("pressedCell").style.backgroundColor = "#f67f7f";
      }
    }
    readState(pollingFrequency);
}

function startPolling()
{
  setStateCompletion("onState");
  readState(pollingFrequency);
}

setTimeout("startPolling()", pollingFrequency);

          ]]>

        </script>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="limit:VEXContactSensor">
    <form method="POST" action="" name="CustomPower">
      <table width="100%" border="0" cellpadding="5" cellspacing="5">
        <th colspan="2">
          VEX Limit Switch
          <img src="/resources/VEXService.Y2011.M09/VEXService.config.LimitSwitch.png"
             align="right" alt="LimitSwitch" width="125px" height="125px"/>
        </th>
        <tr>
          <th width="20%">
            Name:
          </th>
          <td width="80%">
            <input type="text" name="Name" size="20">
              <xsl:attribute name="value">
                <xsl:value-of select="limit:Name"/>
              </xsl:attribute>
            </input>
          </td>
        </tr>
        <tr class="odd">
          <th width="20%">
            Identifier:
          </th>
          <td width="80%">
            <xsl:value-of select="limit:SensorPort"/>
          </td>
        </tr>
        <tr>
          <th width="20%">
            Pressed:
          </th>
          <td width="80%" id="pressedCell">
            <xsl:choose>
              <xsl:when test='limit:Pressed = "true"'>
                <xsl:attribute name="bgcolor">
                  <xsl:text disable-output-escaping="yes">#9ecba3</xsl:text>
                </xsl:attribute>
              </xsl:when>
              <xsl:otherwise>
                <xsl:attribute name="bgcolor">
                  <xsl:text disable-output-escaping="yes">#f67f7f</xsl:text>
                </xsl:attribute>
              </xsl:otherwise>
            </xsl:choose>
            <xsl:value-of select="limit:Pressed"/>
          </td>
        </tr>
        <tr class="odd">
          <th width="20%">
            TimeStamp:
          </th>
          <td width="80%" id="timestampCell">
            <xsl:value-of select="limit:TimeStamp"/>
          </td>
        </tr>
      </table>
      <input id="Button2" type="reset" value="Reset" name="buttonReset" title="Reset Configuration"/>
      <input type="submit" value="Set Values" name="SetValues" title="Set The Values"/>
    </form>
  </xsl:template>

</xsl:stylesheet>
